<?php
namespace sms;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Sendsms{
    /**
     * 发送短信验证码
     * @param string $phone  要发送的目标手机号
     * @param string $signName  短信签名名称。请在阿里云控制台签名管理页面[签名名称]一列查看。
     * @param string $templateCode  短信模板ID。请在控制台模板管理页面[模板CODE]一列查看。
     * @param string $value  发送的验证码的值
     * @return array
     */
    public static function sendVfcode($phone, $signName, $templateCode, $value){
        AlibabaCloud::accessKeyClient('LTAIyXCRgXJ8FIhz', '5OPwWPmiixRrY0vCKLIZPFYFI3Ni9A') // 阿里云配置的AccessKey ID和AccessKeySecret
        ->regionId('cn-chengdu') // replace regionId as you need
        ->asGlobalClient();
    
        try {
            $result = AlibabaCloud::rpcRequest()
            ->product('Dysmsapi')
            ->scheme('https') // https | http
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->options([
                'query' => [
                    'PhoneNumbers' => $phone,
                    'SignName' => $signName,
                    'TemplateCode' => $templateCode,
                    'TemplateParam' => json_encode(['code'=>$value]),
                ],
            ])
            ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

    

    /**
     * 发送短信通知
     * @param string $phone  要发送的目标手机号
     * @param string $signName  短信签名名称。请在阿里云控制台签名管理页面[签名名称]一列查看。
     * @param string $templateCode  短信模板ID。请在控制台模板管理页面[模板CODE]一列查看。
     * @param array $param  模版中的变量值
     * @return array
     */
    public static function sendMessage($phone, $signName, $templateCode, $param){
        AlibabaCloud::accessKeyClient('LTAIyXCRgXJ8FIhz', '5OPwWPmiixRrY0vCKLIZPFYFI3Ni9A') // 阿里云配置的AccessKey ID和AccessKeySecret
        ->regionId('cn-chengdu') // replace regionId as you need
        ->asGlobalClient();
    
        try {
            $result = AlibabaCloud::rpcRequest()
            ->product('Dysmsapi')
            ->scheme('https') // https | http
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->options([
                'query' => [
                    'PhoneNumbers' => $phone,
                    'SignName' => $signName,
                    'TemplateCode' => $templateCode,
                    'TemplateParam' => json_encode($param),
                ],
            ])
            ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }
}